/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var sendReqNotif = getXmlHttpRequestObject();
var receiveReqNotif = getXmlHttpRequestObject();
var xmlResponseNotif;

function $(id) {
    return document.getElementById(id);
}

function initNotif() {
    getNumNotif();
}

function sendNotif(username,message) {
    // fungsi untuk kirim chat via ajax
    sendReqNotif.open("GET","../ajax/send_notif.php?to="+username+"&message="+message,false);
    sendReqNotif.send();
}

function getNumNotif() {
    // fungsi untuk menerima message via ajax
    receiveReqNotif.open("GET","ajax/receive_notif.php?mode=num",true);
    receiveReqNotif.send();

    receiveReqNotif.onreadystatechange=function()
    {
        if (receiveReqNotif.readyState==4 && receiveReqNotif.status==200)
        {
            $("notif_number").innerHTML = receiveReqNotif.responseText;
            setTimeout('getNumNotif();',2000);
        }
    }
}

function getNotif() {
    // fungsi untuk menerima message via ajax
    receiveReqNotif.open("GET","ajax/receive_notif.php?mode=message",true);
    receiveReqNotif.send();

    receiveReqNotif.onreadystatechange=function()
    {
        if (receiveReqNotif.readyState==4 && receiveReqNotif.status==200)
        {
            //alert(receiveReq.responseText);
            xmlResponseNotif = receiveReqNotif.responseXML;
            parseXMLNotif(xmlResponseNotif);    
        }
    }
}

function parseXMLNotif(objXML) {
    $("notif_content_message").innerHTML = "";
    for (i=0;i<objXML.getElementsByTagName("count")[0].childNodes[0].nodeValue;i++) {
        $("notif_content_message").innerHTML += objXML.getElementsByTagName("message")[i].childNodes[0].nodeValue+"\n";
    }
}

function toogleNotif() {
    if ($("notif_content").className == "chat_list_active") {
        $("notif_content").setAttribute("class", "chat_list_inactive");
    }
    else {
        $("notif_content").setAttribute("class", "chat_list_active");
        getNotif();
    }
}
